package o;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Build.VERSION;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class uk implements uc {
    private static final Config ˋ = Config.ARGB_8888;
    private int ʻ;
    private long ʼ;
    private int ʽ;
    private final um ˊ;
    private int ˊॱ;
    private final long ˎ;
    private final c ˏ;
    private final Set<Config> ॱ;
    private long ॱॱ;
    private int ᐝ;

    interface c {
        void ˎ(Bitmap bitmap);

        void ˏ(Bitmap bitmap);
    }

    static final class e implements c {
        e() {
        }

        public void ˎ(Bitmap bitmap) {
        }

        public void ˏ(Bitmap bitmap) {
        }
    }

    uk(long j, um umVar, Set<Config> set) {
        this.ˎ = j;
        this.ʼ = j;
        this.ˊ = umVar;
        this.ॱ = set;
        this.ˏ = new e();
    }

    public uk(long j) {
        this(j, ॱॱ(), ᐝ());
    }

    public long ˏ() {
        return this.ʼ;
    }

    public synchronized void ˏ(Bitmap bitmap) {
        if (bitmap == null) {
            throw new NullPointerException("Bitmap must not be null");
        } else if (bitmap.isRecycled()) {
            throw new IllegalStateException("Cannot pool recycled bitmap");
        } else if (bitmap.isMutable() && ((long) this.ˊ.ॱ(bitmap)) <= this.ʼ && this.ॱ.contains(bitmap.getConfig())) {
            int ॱ = this.ˊ.ॱ(bitmap);
            this.ˊ.ˏ(bitmap);
            this.ˏ.ˎ(bitmap);
            this.ʽ++;
            this.ॱॱ = ((long) ॱ) + this.ॱॱ;
            if (Log.isLoggable("LruBitmapPool", 2)) {
                Log.v("LruBitmapPool", "Put bitmap in pool=" + this.ˊ.ˋ(bitmap));
            }
            ॱ();
            ˋ();
        } else {
            if (Log.isLoggable("LruBitmapPool", 2)) {
                Log.v("LruBitmapPool", "Reject bitmap from pool, bitmap: " + this.ˊ.ˋ(bitmap) + ", is mutable: " + bitmap.isMutable() + ", is allowed config: " + this.ॱ.contains(bitmap.getConfig()));
            }
            bitmap.recycle();
        }
    }

    private void ˋ() {
        ˊ(this.ʼ);
    }

    @NonNull
    public Bitmap ˊ(int i, int i2, Config config) {
        Bitmap ˋ = ˋ(i, i2, config);
        if (ˋ == null) {
            return ˎ(i, i2, config);
        }
        ˋ.eraseColor(0);
        return ˋ;
    }

    @NonNull
    public Bitmap ॱ(int i, int i2, Config config) {
        Bitmap ˋ = ˋ(i, i2, config);
        if (ˋ == null) {
            return ˎ(i, i2, config);
        }
        return ˋ;
    }

    @NonNull
    private static Bitmap ˎ(int i, int i2, @Nullable Config config) {
        if (config == null) {
            config = ˋ;
        }
        return Bitmap.createBitmap(i, i2, config);
    }

    @TargetApi(26)
    private static void ˏ(Config config) {
        if (VERSION.SDK_INT >= 26 && config == Config.HARDWARE) {
            throw new IllegalArgumentException("Cannot create a mutable Bitmap with config: " + config + ". Consider setting Downsampler#ALLOW_HARDWARE_CONFIG to false in your RequestOptions and/or in GlideBuilder.setDefaultRequestOptions");
        }
    }

    @Nullable
    private synchronized Bitmap ˋ(int i, int i2, @Nullable Config config) {
        Bitmap ॱ;
        ˏ(config);
        ॱ = this.ˊ.ॱ(i, i2, config != null ? config : ˋ);
        if (ॱ == null) {
            if (Log.isLoggable("LruBitmapPool", 3)) {
                Log.d("LruBitmapPool", "Missing bitmap=" + this.ˊ.ˎ(i, i2, config));
            }
            this.ʻ++;
        } else {
            this.ᐝ++;
            this.ॱॱ -= (long) this.ˊ.ॱ(ॱ);
            this.ˏ.ˏ(ॱ);
            ˋ(ॱ);
        }
        if (Log.isLoggable("LruBitmapPool", 2)) {
            Log.v("LruBitmapPool", "Get bitmap=" + this.ˊ.ˎ(i, i2, config));
        }
        ॱ();
        return ॱ;
    }

    private static void ˋ(Bitmap bitmap) {
        bitmap.setHasAlpha(true);
        ˊ(bitmap);
    }

    @TargetApi(19)
    private static void ˊ(Bitmap bitmap) {
        if (VERSION.SDK_INT >= 19) {
            bitmap.setPremultiplied(true);
        }
    }

    public void ˎ() {
        if (Log.isLoggable("LruBitmapPool", 3)) {
            Log.d("LruBitmapPool", "clearMemory");
        }
        ˊ(0);
    }

    @SuppressLint({"InlinedApi"})
    public void ˊ(int i) {
        if (Log.isLoggable("LruBitmapPool", 3)) {
            Log.d("LruBitmapPool", "trimMemory, level=" + i);
        }
        if (i >= 40 || (VERSION.SDK_INT >= 23 && i >= 20)) {
            ˎ();
        } else if (i >= 20 || i == 15) {
            ˊ(ˏ() / 2);
        }
    }

    private synchronized void ˊ(long j) {
        while (this.ॱॱ > j) {
            Bitmap ˊ = this.ˊ.ˊ();
            if (ˊ == null) {
                if (Log.isLoggable("LruBitmapPool", 5)) {
                    Log.w("LruBitmapPool", "Size mismatch, resetting");
                    ˊ();
                }
                this.ॱॱ = 0;
            } else {
                this.ˏ.ˏ(ˊ);
                this.ॱॱ -= (long) this.ˊ.ॱ(ˊ);
                this.ˊॱ++;
                if (Log.isLoggable("LruBitmapPool", 3)) {
                    Log.d("LruBitmapPool", "Evicting bitmap=" + this.ˊ.ˋ(ˊ));
                }
                ॱ();
                ˊ.recycle();
            }
        }
    }

    private void ॱ() {
        if (Log.isLoggable("LruBitmapPool", 2)) {
            ˊ();
        }
    }

    private void ˊ() {
        Log.v("LruBitmapPool", "Hits=" + this.ᐝ + ", misses=" + this.ʻ + ", puts=" + this.ʽ + ", evictions=" + this.ˊॱ + ", currentSize=" + this.ॱॱ + ", maxSize=" + this.ʼ + "\nStrategy=" + this.ˊ);
    }

    private static um ॱॱ() {
        if (VERSION.SDK_INT >= 19) {
            return new un();
        }
        return new ua();
    }

    @TargetApi(26)
    private static Set<Config> ᐝ() {
        Set hashSet = new HashSet(Arrays.asList(Config.values()));
        if (VERSION.SDK_INT >= 19) {
            hashSet.add(null);
        }
        if (VERSION.SDK_INT >= 26) {
            hashSet.remove(Config.HARDWARE);
        }
        return Collections.unmodifiableSet(hashSet);
    }
}
